<template>
  <el-dialog
    :title="!dataForm.orderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="userId">
      <el-input v-model="dataForm.userId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="actId">
      <el-input v-model="dataForm.actId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="deliveryAddrId">
      <el-input v-model="dataForm.deliveryAddrId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="goodsCount">
      <el-input v-model="dataForm.goodsCount" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="goodsPrice">
      <el-input v-model="dataForm.goodsPrice" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="goodsChannel">
      <el-input v-model="dataForm.goodsChannel" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="status">
      <el-input v-model="dataForm.status" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="payDate">
      <el-input v-model="dataForm.payDate" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orderId: 0,
          userId: '',
          actId: '',
          goodsId: '',
          deliveryAddrId: '',
          goodsName: '',
          goodsCount: '',
          goodsPrice: '',
          goodsChannel: '',
          status: '',
          createDate: '',
          payDate: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          actId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          goodsId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          deliveryAddrId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          goodsCount: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          goodsPrice: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          goodsChannel: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createDate: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          payDate: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderId) {
            this.$http({
              url: this.$http.adornUrl(`/orders/order/info/${this.dataForm.orderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.order.userId
                this.dataForm.actId = data.order.actId
                this.dataForm.goodsId = data.order.goodsId
                this.dataForm.deliveryAddrId = data.order.deliveryAddrId
                this.dataForm.goodsName = data.order.goodsName
                this.dataForm.goodsCount = data.order.goodsCount
                this.dataForm.goodsPrice = data.order.goodsPrice
                this.dataForm.goodsChannel = data.order.goodsChannel
                this.dataForm.status = data.order.status
                this.dataForm.createDate = data.order.createDate
                this.dataForm.payDate = data.order.payDate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/orders/order/${!this.dataForm.orderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orderId': this.dataForm.orderId || undefined,
                'userId': this.dataForm.userId,
                'actId': this.dataForm.actId,
                'goodsId': this.dataForm.goodsId,
                'deliveryAddrId': this.dataForm.deliveryAddrId,
                'goodsName': this.dataForm.goodsName,
                'goodsCount': this.dataForm.goodsCount,
                'goodsPrice': this.dataForm.goodsPrice,
                'goodsChannel': this.dataForm.goodsChannel,
                'status': this.dataForm.status,
                'createDate': this.dataForm.createDate,
                'payDate': this.dataForm.payDate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
